<template>
	<view>
		<cu-custom bgColor="bg-red" :isBack="true">		<block slot="backText"></block>
			<block slot="content">申请升级</block>
		</cu-custom>
		<view class="">
			<view class="cu-form-group margin-top">
				<view class="title">级别</view>
				<picker @change="LevelPickerChange" :value="levelIndex" :range="config.level" :range-key="'name'" v-if="config">
					<view class="picker">
						{{levelIndex>-1?config.level[levelIndex].name:'请选择申请级别'}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<view class="title">省市</view>
				<cityPicker ref="cityPicker"></cityPicker>
			</view>
			<view class="cu-form-group">
				<view class="title">姓名</view>
				<input placeholder="请输入姓名" name="realname" v-model="formData.realname" class="text-right"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">手机号</view>
				<input placeholder="请输入手机号" name="mobile" v-model="formData.mobile" class="text-right"></input>
			</view>

			<view class="cu-form-group margin-top">
				<view class="title">推荐人</view>
				<input placeholder="请输入推荐人ID,一经确认不可更改" name="channel_pid" v-model="formData.channel_pid" class="text-right" :disabled="reset"></input>
			</view>
			<!-- <view class="cu-form-group ">
				<view class="title">配送产品</view>
				<picker @change="LevelPickerChange" :value="levelIndex" :range="picker">
					<view class="picker">
						{{levelIndex>-1?picker[levelIndex]:'请选择'}}
					</view>
				</picker>
			</view> -->
		</view>
		<view class="text-gray text-center margin-top">资料一经提交无法修改，请核对仔细后再提交！</view>
		<view class="margin-top-xl margin-lr padding-top-xl" v-if="!submitIng">
			<view class="cu-btn lg bg-yellow round w100" @tap="submitApply">确认提交</view>
		</view>
		<view class="margin-top-xl margin-lr padding-top-xl" v-else>
			<view class="cu-btn lg bg-yellow round w100">
				<text class="cu-load loading"></text>
				提交中...</view>
		</view>
	</view>
</template>

<script>
	import cityPicker from '@/components/shop/cityPicker.vue'
	export default {
		components: {
			cityPicker
		},
		data() {
			return {
				CLOUDPATH: this.$config.cloudPath,
				theme: this.$config.THEME(),
				config: null,
				formData: {
					current_level: 0,
					realname: '',
					mobile: '',
					channel_pid: '',
					province_id: 0,
					city_id: 0,
					area_id: 0,
				},
				levelIndex: 0,
				submitIng: false,
				reset:false
			}
		},
		onLoad(options) {
			if (this.$common.loginAuth(true)) {
				this.$common.getMember().then((member) => {
					console.log(member)
					this.member = member
					this.formData.mobile = member.mobile
					this.formData.realname = member.realname
					this.formData.channel_pid = parseInt(member.channel_pid)>0?member.channel_pid:''
					//如果有默认上级，不允许修改
					if(this.formData.channel_pid) this.reset = true
				})
				this.getConfig()
				this.$nextTick((r) => {
					this.$refs.cityPicker.setSelect({
						provinces_id: -1,
						city_id: -1,
						area_id: -1,
					})
				})
			}
		},
		methods: {
			LevelPickerChange(e) {
				// console.log(e)
				this.levelIndex = e.detail.value
			},
			submitApply() {
				if(this.submitIng) return 
				let formData = this.formData
				formData.current_level = this.config.level[this.levelIndex].level
				if (!formData.realname.trim()) {
					this.$common.showError('姓名不能为空')
					return
				}
				if (!(/^1(2|3|4|5|6|7|8|9)\d{9}$/.test(formData.mobile))) {
					this.$common.showError('请输入正确的手机号')
					return
				}
				let select = this.$refs.cityPicker.getSelect()
				formData.province_id = select.provinces_id
				formData.city_id = select.city_id
				formData.area_id = select.area_id
				if (formData.province_id < 0 || formData.city_id < 0 || formData.area_id < 0) {
					this.$common.showError('请输入正确的省市区')
					return
				}
				if (!formData.channel_pid) {
					this.$common.showError('请输入正确的推荐人id')
					return
				}
				if(formData.current_level < 2){
					this.$common.showError('请输入正确的级别')
					return
				}
				this.submitIng = true
				// console.log(formData)
				this.$http
					.post('channel_apply', formData)
					.then((response) => {
						// console.log(response)
						this.submitIng = false
						this.$common.showSuccess('提交成功，请耐心等待审核',(r)=>{
							uni.navigateTo({
								url:'/pages/member/index'
							})
						})
					})
					.catch((response) => {
						this.submitIng = false
						console.log(response)
					})
			},
			getConfig() {
				uni.showLoading({
					title: '加载中...'
				})
				this.$http
					.get('channel_apply_config')
					.then((response) => {
						this.config = response.data.data
						// console.log(response)
						uni.hideLoading()
					})
					.catch((response) => {
						// console.log(response)
						uni.hideLoading()
					})
			}
		},
	}
</script>

<style>
</style>
